/*

 * Licensed to the Apache Software Foundation (ASF) under one

 * or more contributor license agreements.  See the NOTICE file

 * distributed with this work for additional information

 * regarding copyright ownership.  The ASF licenses this file

 * to you under the Apache License, Version 2.0 (the

 * "License"); you may not use this file except in compliance

 * with the License.  You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package com.bff.gaia.unified.sdk.nexmark.queries.sql;



import com.bff.gaia.unified.sdk.coders.Coder;

import com.bff.gaia.unified.sdk.extensions.sql.SqlTransform;

import com.bff.gaia.unified.sdk.metrics.Counter;

import com.bff.gaia.unified.sdk.metrics.Metrics;

import com.bff.gaia.unified.sdk.nexmark.model.Bid;

import com.bff.gaia.unified.sdk.nexmark.model.Event;

import com.bff.gaia.unified.sdk.nexmark.model.sql.SelectEvent;

import com.bff.gaia.unified.sdk.nexmark.queries.NexmarkQueryTransform;

import com.bff.gaia.unified.sdk.nexmark.queries.NexmarkQueryUtil;

import com.bff.gaia.unified.sdk.schemas.transforms.Convert;

import com.bff.gaia.unified.sdk.transforms.DoFn;

import com.bff.gaia.unified.sdk.transforms.Filter;

import com.bff.gaia.unified.sdk.transforms.PTransform;

import com.bff.gaia.unified.sdk.transforms.ParDo;

import com.bff.gaia.unified.sdk.values.PCollection;

import com.bff.gaia.unified.sdk.values.PInput;

import com.bff.gaia.unified.sdk.values.Row;



import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;



/**

 * Query 0: Pass events through unchanged.

 *

 * <p>This measures the overhead of the Unified SQL implementation and test harness like conversion

 * from Java model classes to Unified records.

 *

 * <p>{@link Bid} events are used here at the moment, ås they are most numerous with default

 * configuration.

 */

public class SqlQuery0 extends NexmarkQueryTransform<Bid> {



  private static final PTransform<PInput, PCollection<Row>> QUERY =

      SqlTransform.query("SELECT * FROM PCOLLECTION");



  public SqlQuery0() {

    super("SqlQuery0");

  }



  @Override

  public PCollection<Bid> expand(PCollection<Event> allEvents) {

    PCollection<Row> rows =

        allEvents

            .apply(Filter.by(NexmarkQueryUtil.IS_BID))

            .apply(getName() + ".SelectEvent", new SelectEvent(Event.Type.BID));



    return rows.apply(getName() + ".Serialize", logBytesMetric(rows.getCoder()))

        .apply(QUERY)

        .apply(Convert.fromRows(Bid.class));

  }



  private PTransform<? super PCollection<Row>, PCollection<Row>> logBytesMetric(

      final Coder<Row> coder) {



    return ParDo.of(

        new DoFn<Row, Row>() {

          private final Counter bytesMetric = Metrics.counter(name, "bytes");



          @ProcessElement

          public void processElement(@Element Row element, OutputReceiver<Row> o)

              throws IOException {

            ByteArrayOutputStream outStream = new ByteArrayOutputStream();

            coder.encode(element, outStream, Coder.Context.OUTER);

            byte[] byteArray = outStream.toByteArray();

            bytesMetric.inc((long) byteArray.length);

            ByteArrayInputStream inStream = new ByteArrayInputStream(byteArray);

            Row row = coder.decode(inStream, Coder.Context.OUTER);

            o.output(row);

          }

        });

  }

}